﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;

namespace PatientInformationExtract.MainInterface.Views
{
    /// <summary>
    /// Interaction logic for NumericUpDown.xaml
    /// </summary>
    public partial class NumericUpDown : INotifyPropertyChanged
    {

        public NumericUpDown()
        {
            InitializeComponent();
            MinValue = null;
            MaxValue = null;
            ValueText.DataContext = this;
        }

        public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(int), typeof(NumericUpDown), new FrameworkPropertyMetadata(100, new PropertyChangedCallback(ValuePropertyChangedCallback)));

        private static void ValuePropertyChangedCallback(DependencyObject sender, DependencyPropertyChangedEventArgs arg)
        {
            if (sender != null && sender is NumericUpDown)
            {
                NumericUpDown numeric = sender as NumericUpDown;
                numeric.NotifyPropertyChanged("Value");
            }
        }

        [Category("Common")]
        public int Value
        {
            get
            {
                return (int)this.GetValue(ValueProperty);
            }
            set
            {
                this.SetValue(ValueProperty, value);
            }
        }


        public int Increment { get; set; }

        public int? MaxValue { get; set; }

        public int? MinValue { get; set; }

        private void UpButton_Click(object sender, RoutedEventArgs e)
        {
            int newValue = (Value + Increment);
            if (MaxValue != null)
            {
                if (newValue > MaxValue)
                {
                    Value = (int)MaxValue;
                }
                else
                {
                    Value = newValue;
                }
            }
            else
            {
                Value = newValue;
            }
        }

        private void DownButton_Click(object sender, RoutedEventArgs e)
        {
            int newValue = (Value - Increment);
            if (MinValue != null)
            {
                if (newValue < MinValue)
                {
                    Value = (int)MinValue;
                }
                else
                {
                    Value = newValue;
                }
            }
            else
            {
                Value = newValue;
            }
        }

        private void ValueText_LostFocus(object sender, RoutedEventArgs e)
        {
            try
            {
                Value = int.Parse(ValueText.Text);
                if (MaxValue != null)
                {
                    if (Value > MaxValue)
                    {
                        Value = (int)MaxValue;
                    }
                }
                if (MinValue != null)
                {
                    if (Value < MinValue)
                    {
                        Value = (int)MinValue;
                    }
                }
            }
            catch
            {
                Value = 0;
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        public void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion
    }

    [ValueConversion(typeof(int), typeof(string))]
    public class IntegerValueConverter : IValueConverter
    {
        #region IValueConverter Members
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            try
            {
                return value.ToString();
            }
            catch (Exception)
            {
                return "0";
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            try
            {
                return int.Parse((string)value);
            }
            catch (Exception)
            {
                return 0;
            }
        }
        #endregion

    }
}
